package com.leo.ibatis.demo.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.leo.ibatis.demo.criteria.SignonCriteria;
import com.leo.ibatis.demo.domain.Signon;
import com.leo.ibatis.demo.service.SignonService;

public class SignonController extends MultiActionController {

	private SignonService signonService;

	public void setSignonService(SignonService signonService) {
		this.signonService = signonService;
	}

	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		SignonCriteria signonCriteria = new SignonCriteria();
		bind(request, signonCriteria);
		
		List<Signon> signonList = signonService.listOnPage(signonCriteria);
		
		ModelAndView mav = new ModelAndView("signon/list");
		mav.addObject("signonList", signonList);
		mav.addObject("signonCriteria", signonCriteria);
		
		return mav;
	}

	public ModelAndView detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		SignonCriteria signonCriteria = new SignonCriteria();
		bind(request, signonCriteria);
			
		String username = ServletRequestUtils.getRequiredStringParameter(request, "username");

		Signon signon = signonService.select(username);
		
		ModelAndView mav = new ModelAndView("signon/detail");
		mav.addObject("signon", signon);
		mav.addObject("signonCriteria", signonCriteria);
		
		return mav;
	}

	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String username = ServletRequestUtils.getRequiredStringParameter(request, "username");

		signonService.delete(username);
		
		return new ModelAndView("redirect:signon.do");
	}

}
